package com.kongshidian.ui.view;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.kongshidian.R;

/** 商品列表溢出菜单 */
public class OverflowMenu extends FrameLayout {
	private Activity activity;
	
	private View mBackground;
	private ListView mLv;
	
	private Animation mFadeIn, mFadeOut, mPushIn, mPushOut;
	
	public OverflowMenu(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	private OverflowMenu(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	private OverflowMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		activity = (Activity) context;
		
		// layoutparam
		FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		param.topMargin = 2 * getResources().getDimensionPixelSize(R.dimen.titlebar_default_height);
		setLayoutParams(param);
		// inflater view
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.overflowmenu, this);
		// add to container
		ViewGroup container = ((ViewGroup) activity.findViewById(android.R.id.content));
		container.addView(this);
		
		
		mBackground = findViewById(R.id.overflow_bg);
		mBackground.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				close();
			}
		});
		mLv = (ListView) findViewById(R.id.overflow_lv);
		
		mFadeIn = AnimationUtils.loadAnimation(context, R.anim.fade_in);
		mFadeOut = AnimationUtils.loadAnimation(context, R.anim.fade_out);
		mPushIn = AnimationUtils.loadAnimation(context, R.anim.push_down_in_no_alpha);
		mPushOut = AnimationUtils.loadAnimation(context, R.anim.push_down_out_no_alpha);
		
	}

	
	/** 设置选项 */
	public OverflowMenu setEntries(Object[] t) {
		return setEntries(Arrays.asList(t));
	}
	
	/** 设置选项 */
	public OverflowMenu setEntries(List<?> t) {
		mLv.setAdapter(new ArrayAdapter<>(activity, android.R.layout.simple_list_item_1, t));
		
		return this;
	}
	
	/** 事件 */
	public OverflowMenu setOnItemClickListener(OnItemClickListener listener) {
		mLv.setOnItemClickListener(listener);
		return this;
	}
	
	
	/** 显示 */
	public void open() {
		if (isShown()) return;
		
		mBackground.setAnimation(mFadeIn);
		mLv.setAnimation(mPushIn);
		
		mFadeIn.start();
		mPushIn.start();
		
		mBackground.setVisibility(View.VISIBLE);
		mLv.setVisibility(View.VISIBLE);
	}
	
	/** 隐藏 */
	public void close() {
		if (!isShown()) return;
		
		mBackground.setAnimation(mFadeOut);
		mLv.setAnimation(mPushOut);
		
		mFadeOut.start();
		mPushOut.start();
		
		mBackground.setVisibility(View.INVISIBLE);
		mLv.setVisibility(View.INVISIBLE);
	}

	@Override
	public boolean isShown() {
		return mBackground.isShown();
	}
}
